﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TripsLogic.controller;

namespace TripsLogic.master.uc
{
    public partial class ucHeader : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                loadLang();
                loadSlider();
                loadMenu();
            }
        }

        private void loadMenu()
        {
            litMenu.Text = "<ul id='menu'>";
            DataTable dt = MenuController.getMenus("2", "");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string menuId = dt.Rows[i]["Id"].ToString();
                    string groupId = dt.Rows[i]["GroupId"].ToString();
                    string action_url = dt.Rows[i]["ActionUrl"].ToString();
                    string parent_id = dt.Rows[i]["ParentId"].ToString();
                    string text = dt.Rows[i]["Text"].ToString();

                    if (action_url.ToLower().Contains(System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]).ToLower()))
                    {
                        litMenu.Text += "<li id='nav" + (i + 1) + "' class='active'>";
                    }
                    else
                    {
                        litMenu.Text += "<li id='nav" + (i + 1) + "'>";
                    }
                   
                    litMenu.Text += "<a href='" + action_url.Split('.')[0] + "'> " + text + "</a>";
                    if (text.ToLower() == "account")
                    {
                        if (Session["UserId"] != null && Session["UserId"].ToString() != "")
                        {
                            string sProfile = LangController.getLng("litProfile.Text", "Profile");
                            string sChangePassword = LangController.getLng("litChangePassword.Text", "Change password");
                            string sAddress = LangController.getLng("litAddress.Text", "Address");
                            string sMyTrip = LangController.getLng("litMyTrip.Text", "My trip");
                            string sLogout = LangController.getLng("litLogout.Text", "Log out");

                            litMenu.Text += "<ul class='submenu1'><li><a href='" + sProfile.ToLower().Replace(' ', '-') + "'>" + sProfile + "</a></li>";
                            litMenu.Text += "<li><a href='" + sChangePassword.ToLower().Replace(' ', '-') + "'>" + sChangePassword + "</a></li>";
                            litMenu.Text += "<li><a href='" + sAddress.ToLower().Replace(' ', '-') + "'>" + sAddress + "</a></li>";
                            litMenu.Text += "<li><a href='" + sMyTrip.ToLower().Replace(' ', '-') + "'>" + sMyTrip + "</a></li>";
                            litMenu.Text += "<li><a href='" + sLogout.ToLower().Replace(' ', '-') + "'>" + sLogout + "</a></li></ul>";
                        }
                        else
                        {
                           

                            string slogIn = LangController.getLng("litLogIn.Text", "Log in");
                            string sRegister = LangController.getLng("litRegister.Text", "Register");

                            litMenu.Text += "<ul class='submenu1'><li><a href='" + slogIn.ToLower().Replace(' ', '-') + "'>" + slogIn + "</a></li>";
                            litMenu.Text += "<li><a href='" + sRegister.ToLower().Replace(' ', '-') + "'>" + sRegister + "</a></li></ul>";
                        }
                    }
                    constructSubMenu(groupId, menuId);
                    litMenu.Text += "</li>";

                }
            }

            litMenu.Text += "</ul>";
        }

        private void constructSubMenu(string groupId, string parentId)
        {
            DataTable dt = MenuController.getMenus(groupId, parentId);

            if (dt != null && dt.Rows.Count > 0)
            {
                litMenu.Text += "<ul class='submenu1'>";

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string subMenuId = dt.Rows[i]["Id"].ToString();
                    string subGroupId = dt.Rows[i]["GroupId"].ToString();
                    string action_url = dt.Rows[i]["ActionUrl"].ToString();
                    string parent_id = dt.Rows[i]["ParentId"].ToString();
                    string text = dt.Rows[i]["Text"].ToString();

                    if (action_url.ToLower().Contains(System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]).ToLower()))
                    {
                        litMenu.Text += "<li class='active'>";
                    }
                    else
                    {
                        litMenu.Text += "<li>";
                    }

                    litMenu.Text += "<a href='" + action_url.Split('.')[0] + "'> " + text + "</a>";
                    litMenu.Text += "</li>";

                }
                litMenu.Text += "</ul>";
            }

        }

        private void loadSlider()
        {
            litSlider.Text = "<ul class='pagination'>";
            DataTable dt = SliderController.getSliders("1");

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Id"].ToString().Equals(dt.Rows[0]["Id"].ToString()))
                    {
                        litSlider.Text += "<li class='current'><a href='images/slide/" + dr["DisplayImage"].ToString() + "'></a></li>";
                    }
                    else
                    {
                        litSlider.Text += "<li><a href='images/slide/" + dr["DisplayImage"].ToString() + "'></a></li>";
                    }
                }
            }
            litSlider.Text += "</ul>";
        }

        private void loadLang()
        {
            
        }
    }
}